import store from '@/store/index.js'
let that = this;


// 返回时间戳
export function getTime(){
    let d = new Date()
    return d.getTime()
}

// 世界世界转北京时间
export function newTime(date){
  let a = new Date(date),
  format = (time) =>{
    let y = time.getFullYear(),
        m = (time.getMonth()+1)>9?time.getMonth()+1:'0'+time.getMonth()+1,
        d = time.getDate()>9?time.getDate():'0'+time.getDate(),
        h = time.getHours()>9?time.getHours():'0'+time.getHours(),
        mm = time.getMinutes()>9?time.getMinutes():'0'+time.getMinutes(),
        s = time.getSeconds()>9?time.getSeconds():'0'+time.getSeconds();
        return y + '-' + m + '-' + d + ' ' + h + ':' + mm + ':' + s
  };
  let b = format(a)
  return b
}

// 参数排序
export function sortParams(data){
  let keyArr = [],str = '';
  for(let i in data){
      keyArr.push(i)
  }
  keyArr = keyArr.sort()
  for(let i in keyArr){
      if(str==''){
          str += keyArr[i] + "=" + data[keyArr[i]]
      }
      else{
          str += '&'+keyArr[i] + "=" +data[keyArr[i]]
      }
  }
  return str
}

export function stringReplace(str){
   let newStr = str.replace(/^\d\-/g,'')
   .replace(/</g, "&lt;")
   .replace(/\+/g, "&#043;")
   .replace(/>/g, "&gt;");
   if(newStr.length<=4){
    newStr = newStr.replace(/-/g, "&#8722;")
   }
   return newStr
} 

// 获取app传过来的参数
export function getVal(){
    let u = navigator.userAgent,valueArr = {} // 拿取userAgent
    if(process.env.NODE_ENV==='development'){
      u = '&from_TV;api_eva_app_id=aa2e94809c6d442a8056008656d6f1ec;user_grade_id=3;api_eva_app_code=app_cuc_xxtbkt_xa;api_eva_app_secret=7f392fb84c0240b5b4ef4fbd13055e60;user_uid=472fdbd5de6f47e1af408885eea61d86'
    }
    console.log('u',u)
    let Val;
    if(u.indexOf('&from_TV')>-1){
      Val = u.match(/&from_TV\;(.*)/) 
      store.state.isTV = true;
    }
    else{
      Val = u.match(/&from_app\;(.*)/) 
    }
      //  console.log('正则截图出来的参数',Val)
       if(Val){
        let userfulVal = Val[1].split(";") // 根据 ',' 拆分为数组
        userfulVal.forEach(i => {
          let n = i.slice(11)
          let key = i.split("=")[0] // 根据 '=' 拆分为 key和 value
          let value = i.split("=")[1]
          if(key=="user_token"){
            value = n
          }
          valueArr[key] = value // 向数组里面添加keyvalue值
        })
       }
    
    return valueArr||0    
}